import dayjs from 'dayjs'
import { formatEmergency } from '@/views/oa/officialDoc/dispatch/emergencyTool.ts'

// 正常编辑页面的表头
export const normalCols = [
  { type: "index", align: "center", title: "序号", width: 50, 
    customRender: ({ index }) => index += 1
  },
  { title: "申请时间", width: 160, dataIndex: "applicationdate", align: "center", 
    customRender: ({ text }) => (text || '').split(' ')[0]
  },
  { title: "车牌号", width: 140, dataIndex: "vehiclenumber", align: "center", },
  { title: "所属部门", width: 140, dataIndex: "ssdeptname", align: "center" },
  { title: "申请类型", width: 140, dataIndex: "applicationtypes", align: "center", },
  { title: "申请人", width: 140, dataIndex: "applicantname", align: "center" },
  { title: "申请人电话", width: 140, dataIndex: "applicantphone", align: "center", },
  { title: "维保机构", width: 130, dataIndex: "fkactmechanismname", align: "center", },
  { title: "维保事由", width: 260, dataIndex: "inspectionreason", },
  { title: "实际完成时间", width: 160, dataIndex: "actualcomdate", align: "center", 
    customRender: ({ text }) => (text || '').split(' ')[0]
  },
]

// 维修分析的表头
export const wxCols = [
  { type: "index", align: "center", title: "序号", width: 50, 
    customRender: ({ index }) => index += 1
  },
  { title: "车牌号", width: 140, dataIndex: "vehiclenumber", align: "center", },
  { title: "所属部门", width: 140, dataIndex: "ssdeptname", align: "center" },
  { title: "维修次数", width: 160, dataIndex: "frequency", align: "center", },
]

// 保养分析的表头
export const byCols = [
  { type: "index", align: "center", title: "序号", width: 50, 
    customRender: ({ index }) => index += 1
  },
  { title: "车牌号", width: 140, dataIndex: "vehiclenumber", align: "center", },
  { title: "所属部门", width: 140, dataIndex: "ssdeptname", align: "center" },
  { title: "上次保养机构", width: 160, dataIndex: "fkmechanismname", align: "center", },
  { title: "保养完成时间", width: 160, dataIndex: "actualcomdate", align: "center", 
    customRender: ({ text }) => (text || '').split(' ')[0]
  },
  { title: "距下次保养天数", width: 160, dataIndex: "actualcomdate", align: "center", 
    customRender: ({ record }) => {
      return nextToNowDays(record.actualcomdate)
    }
  },
  { title: "距下次保养里程", width: 160, dataIndex: "curmeliage", align: "center", 
    customRender: ({ record }) => {
      return nextToNowMile(record)
    }
  },
]

// 计算下一次保养距现在的天数
function nextToNowDays(nextDate: any) {
  if(nextDate) {
    let deadDay = dayjs(nextDate).add(1, 'year').subtract(1, 'day').diff(dayjs(), 'day') + 1
    let deadDay1 = deadDay < 0 ? `已超期${ Math.abs(deadDay) }` : deadDay
    return deadDay <= 30 ? formatEmergency(4, `${deadDay1}天`) : `${deadDay1}天`
  }
}

// 计算下一次保养的里程
function nextToNowMile(record: object) {
  let { nowCurmeliage = 0, curmeliage = 0 } = record;
  let nextMile = nowCurmeliage - curmeliage
  // 如果上次保养里程大于当前里程，则直接取默认1W公里一保
  nextMile = curmeliage > nowCurmeliage ? 10000 : nextMile
  let nextMile1 = nextMile < 0 ? `已超里程${ nextMile }` : `${nextMile}`
  return nextMile <= 500 ? formatEmergency(4, `${nextMile1}KM`) : `${nextMile1}KM`
}